using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MGDWBear
{
    public static class ScreenManager
    {
        private static List<Screen> screens = new List<Screen>();
        private static bool started = false;
        private static Screen previous = null;

        public static Screen ActiveScreen = null;

        public static void AddScreen(Screen screen)
        {
            if (screens.Any(s => s.Name == screen.Name) == true)
                return;
            screens.Add(screen);
        }

        public static int GetScreenNumber()
        {
            return screens.Count;
        }

        public static Screen GetScreenIndex(int index)
        {
            return screens.ElementAt(index);
        }

        public static void GotoScreen(string name)
        {
            foreach (var screen in screens)
            {
                if (screen.Name == name)
                {
                    previous = ActiveScreen;
                    if (ActiveScreen != null)
                        ActiveScreen.Shutdown();

                    ActiveScreen = screen;
                    if (started) ActiveScreen.Init();
                    return;
                }
            }
        }

        public static void Init()
        {
            started = true;
            if (ActiveScreen != null)
                ActiveScreen.Init();
        }

        public static void GoBack()
        {
            if (previous != null)
            {
                GotoScreen(previous.Name);
                return;
            }
        }

        public static void Update(GameTime gameTime)
        {
            if (started == false) return;
            if (ActiveScreen != null)
                ActiveScreen.Update(gameTime);
        }

        public static void Draw(GameTime gameTime)
        {
            if (started == false) return;
            if (ActiveScreen != null)
                ActiveScreen.Draw(gameTime);
        }
    }
}
